package tools;

import java.awt.Point;
import java.awt.event.MouseEvent;

import tools.shapes.Shape;
import app.DrawingCanvas;


/**
 * The eraser tool allows the user to erase an object on the canvas by clicking on the object.
 */
public class EraserTool extends Tool {
   
  /* Class member variables */
  protected DrawingCanvas canvas;
  protected Point startingMousePosition;

  /****< Constructor >*********************************************************/
  public EraserTool(DrawingCanvas c) {
    if( c != null )
      canvas = c;
    else
      throw new IllegalArgumentException();
  }
  
  public void mouseClicked(MouseEvent e) {
	  startingMousePosition = e.getPoint();

		/* the active object should be temporarily set to null */
		if (canvas.getActiveObject() != null) {
			canvas.getActiveObject().drawBoundingBox(canvas.getimageBufferGraphics());
		}
		canvas.setActiveObject(null);
		/* use some iterator to loop through the canvas object vector */
		
		int vectorLength = canvas.getShapeStack().size();
		for(int count = vectorLength - 1; (canvas.getActiveObject() == null) && (count >= 0); count--){
			/* at this point need a vector operation to pop a new object into context */
			Shape object = canvas.getShape(count);
			if(object.insideBoundingBox(startingMousePosition)){
				canvas.setActiveObject( object );
			}
		}
		if(canvas.getActiveObject() != null) {
			canvas.eraseActiveObject();
		}
	  
	  canvas.redrawAll();
  }
}// end public class SelectTool extends Tool

